package com.coin900.coin.cmd.sop;

import com.google.gson.Gson;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.sop.SopButtonVO;
import com.coin900.coin.service.sop.ISopButtonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新增或修改按钮
 *
 * @author shui
 * @create 2018-01-11
 **/
@Controller
public class SopButtonPutCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        SopButtonVO sopButtonVO = new Gson().fromJson(source, SopButtonVO.class);
        Long id = sopButtonVO.getId();

        if (id == null) {
            // 新增
            sopButtonVO.setCreatedInfor(user);
            this.buttonService.insertSopButton(sopButtonVO);
        } else {
            // 修改
            Map<String, Object> params = new HashMap<>();
            params.put("dataStatus", 1);
            params.put("id", id);
            List<SopButtonVO> sopButtonVOS = this.buttonService.loadSopButton(params);
            if (sopButtonVOS.isEmpty()) {
                respSign.setMsg("按钮不存在");
                respSign.setRespCode(RespCode.FAIL);
                return respSign;
            }
            sopButtonVO.setModifiedInfor(user);
            this.buttonService.updateSopButton(sopButtonVO);
        }

        return respSign;
    }

    @Autowired
    private ISopButtonService buttonService;

    @Override
    public String getCmdCode() {
        return CmdCode.SOP_BUTTON_PUT;
    }
}
